package cn.iocoder.yudao.module.crossborder.order.service.ordershipment;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.order.controller.admin.ordershipment.vo.*;
import cn.iocoder.yudao.module.crossborder.order.dal.dataobject.ordershipment.OrderShipmentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.order.dal.mysql.ordershipment.OrderShipmentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.order.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.crossborder.enums.CrossBorderConstants.*;
import cn.iocoder.yudao.module.crossborder.order.enums.ShipmentTypeEnum;
import cn.iocoder.yudao.module.crossborder.order.enums.DeliveryStatusEnum;

/**
 * 跨境电商订单发货 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OrderShipmentServiceImpl implements OrderShipmentService {

    @Resource
    private OrderShipmentMapper orderShipmentMapper;

    @Override
    public Long createOrderShipment(OrderShipmentSaveReqVO createReqVO) {
        // 插入
        OrderShipmentDO orderShipment = BeanUtils.toBean(createReqVO, OrderShipmentDO.class);
        orderShipmentMapper.insert(orderShipment);

        // 返回
        return orderShipment.getId();
    }

    @Override
    public void updateOrderShipment(OrderShipmentSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderShipmentExists(updateReqVO.getId());
        // 更新
        OrderShipmentDO updateObj = BeanUtils.toBean(updateReqVO, OrderShipmentDO.class);
        orderShipmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderShipment(Long id) {
        // 校验存在
        validateOrderShipmentExists(id);
        // 删除
        orderShipmentMapper.deleteById(id);
    }

    @Override
        public void deleteOrderShipmentListByIds(List<Long> ids) {
        // 删除
        orderShipmentMapper.deleteByIds(ids);
        }


    private void validateOrderShipmentExists(Long id) {
        if (orderShipmentMapper.selectById(id) == null) {
            throw exception(ORDER_SHIPMENT_NOT_EXISTS);
        }
    }

    @Override
    public OrderShipmentDO getOrderShipment(Long id) {
        return orderShipmentMapper.selectById(id);
    }

    @Override
    public PageResult<OrderShipmentDO> getOrderShipmentPage(OrderShipmentPageReqVO pageReqVO) {
        return orderShipmentMapper.selectPage(pageReqVO);
    }

}